package com.xsl.culture.mybasevideoview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.xapp.jjh.logtools.logger.LogUtil;
import com.xsl.culture.R;
import com.xsl.culture.mybasevideoview.model.ChapterListInfo;
import com.xsl.culture.mybasevideoview.utils.DisplayUtils;
import com.xsl.culture.mybasevideoview.utils.ScreenUtil;

import java.util.List;

import static com.xsl.culture.mybasevideoview.MainPlayerActivity.TAG;

public class ChapterSeekBarLayout extends FrameLayout {
    private Paint linePaint;
    //单位是秒
    private int mDuration;
    ChapterListInfo chapterListInfo = null;
    ChapterSeekBar seekBar;

    public ChapterSeekBarLayout(Context context) {
        super(context);
        initPaint();
    }

    public ChapterSeekBarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public ChapterSeekBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    public void setChapterListInfo(ChapterListInfo info, int totalDuration) {
        chapterListInfo = info;
        mDuration = totalDuration / 1000;
        seekBar.setChapterListInfo(chapterListInfo,totalDuration);
        LogUtil.e("实例化initpaint》setChapterListInfo》》"+mDuration+Thread.currentThread().getName());
//        this.postInvalidate();
//        ((View)seekBar.getParent()).invalidate();
//        ((View)seekBar.getParent()).postInvalidate();
//        this.requestLayout();
        forceLayout();

    }


    void initPaint() {
        if (linePaint == null)
        {
            linePaint = new Paint();
            linePaint.setStrokeWidth(1);
            linePaint.setColor(Color.parseColor("#d9c2a7"));
        }
        View view = inflate(getContext(), R.layout.chapter_seekbar_layout,null);
        seekBar = view.findViewById(R.id.seek_bar);
        view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        addView(view);
        invalidate();
        LogUtil.e("实例化initpaint》》》"+mDuration);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        LogUtil.e("实例化initpaint》ondraw》》"+mDuration);
        if (mDuration <= 0) {
            return;
        }
        int w = getWidth();
        int y= ScreenUtil.dp2px(33);
        List<Integer> xList = seekBar.getXList();
        int startX = 0;
        int lineH = ScreenUtil.dp2px(99);
        LogUtil.e("实例化initpaint》xList》》"+xList.size());
        for (int posX : xList) {
            //seekBar左边总是会有预留空间，容错
            if (posX < 10)
                posX = 10;
            //Log.d(TAG,"小白点位置:" + posX);
            int h = DisplayUtils.dp2px(getContext(), 4);
            int height = getHeight();
            Log.d(TAG, "Seekbar>>>xxx h:" + h + " height:"+ height);
            canvas.drawLine(posX,  y,startX,lineH, linePaint);
            startX+= ScreenUtil.dp2px(57);
        }
    }


}
